let global_page = 1,
  global_limit = 5,
  warehouseUrl = '/warehouse';

function loadWarehouse(id) {
  loadWarehouseForm();
  id = id === 'undefined' ? '' : id;
  $.get(warehouseUrl + '/findAllByPage', {page: global_page, limit: global_limit, id: id}, warehouses => {
    if (warehouses.data.length === 0 && global_page > 1) {
      global_page--;
      loadWarehouse();
    } else {
      loadWarehouseTable(warehouses);
      loadWarehouseLayPage(warehouses);
    }
  });
}

function loadWarehouseTable(warehouses) {
  layui.use(['table', 'layer'], () => {
    let table = layui.table,
      layer = layui.layer;
    table.render({
      elem: '#warehouse',
      data: warehouses.data,
      limit: global_limit,
      toolbar: '#warehouse-toolbar',
      cols: [
        [
          {field: 'id', title: '批次编号'},
          {field: 'goods', title: '商品名称'},
          {field: 'price', title: '单价/元'},
          {field: 'count', title: '入库数量'},
          {
            field: 'totalPrice', title: '总价/元', templet: data => {
              return '￥' + data.price;
            }
          },
          {field: 'time', title: '出入库时间'},
          {
            field: 'type', title: '操作类型', templet: data => {
              return data.type === 0 ? '出库' : '入库';
            }
          }
        ]
      ]
    });
    table.on('toolbar(warehouse)', elem => {
      let event = elem.event;
      switch (event) {
        case 'add':
          $('input[name=operator]').val($('.layui-header .layui-nav-item a:eq(0)').text());
          showLayout($('#add-warehouse-layout'));
          break;
        case 'search':
          loadWarehouse($('#search').val());
          break;
      }
    });
  });
}

function loadWarehouseLayPage(warehouses) {
  layui.use('laypage', () => {
    layui.laypage.render({
      elem: 'warehouse-page',
      limit: global_limit,
      limits: [5, 10, 20, 50],
      count: warehouses.count,
      layout: ['count', 'prev', 'page', 'next', 'limit'],
      jump: function (obj, first) {
        if (!first) {
          global_page = obj.curr;
          global_limit = obj.limit;
          loadWarehouse();
        }
      }
    });
  });
}

function loadWarehouseForm() {
  layui.use(['element', 'form', 'layer'], () => {
    let form = layui.form,
      layer = layui.layer;
    form.on('submit(add)', elem => {
      let field = elem.field;
      $.post(warehouseUrl + '/addWarehouse', field, result => {
        result = result > 0 ? '成功' : '失败';
        layer.msg('商品入库' + result + '!');
        hideLayout($('#add-warehouse-layout'));
        loadWarehouse();
      });
    });
  });
}

$('input[name=goods]').click(() => {
  let display = $('ul.goods').css('display');
  if (display === 'block')
    $('.goods').fadeOut();
  else
    $('.goods').fadeIn();
}).bind('input', function () {
  $.get('/goods/findAllGoodsByName', {name: $(this).val()}, names => {
    let liHtml = '';
    if (names.length === 0)
      liHtml = '<li onclick="writeGoodsName()">无数据</li>';
    else
      $.each(names, (i, v) => {
        liHtml += '<li onclick="writeGoodsName($(this))">' + v + '</li>';
      });
    $('.goods').html(liHtml);
  });
});

function writeGoodsName(elem) {
  if (elem !== undefined) $('#goods').val(elem.text());
  $('.goods').fadeOut();
}